# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=graphicsmagick
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.41
pkgrel=2
pkgdesc="An image viewing/manipulation program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="http://www.graphicsmagick.org/"
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-ghostscript"
             "${MINGW_PACKAGE_PREFIX}-libheif"
             "${MINGW_PACKAGE_PREFIX}-libjxl"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "txt2html")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-libltdl"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
         #"${MINGW_PACKAGE_PREFIX}-perl"
optdepends=("${MINGW_PACKAGE_PREFIX}-ghostscript: for Ghostscript support"
            "${MINGW_PACKAGE_PREFIX}-libheif: for HEIF support"
            "${MINGW_PACKAGE_PREFIX}-libjxl: for JPEG XL support")
options=('staticlibs' 'strip' 'libtool')
source=(https://sourceforge.net/projects/graphicsmagick/files/${_realname}/${pkgver}/GraphicsMagick-${pkgver}.tar.xz
        pathtools.c
        pathtools.h
        001-relocate.patch
        002-tiff-transparency.patch)
sha256sums=('b741b11ba86162db4d4ec1b354989a773f73c40722d1148239f6c69c9f04a6aa'
            '08209cbf1633fa92eae7e5d28f95f8df9d6184cc20fa878c99aec4709bb257fd'
            '965d3921ec4fdeec94a2718bc2c85ce5e1a00ea0e499330a554074a7ae15dfc6'
            '73ce65aad39e54b786c332a9aac42d39353af02cf32e3fbfe4e607644cc0a254'
            'e040ba17c50391d03322e9f47ca5876385e8f1a984ec96856276a97f2794be16')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

# =========================================== #

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd GraphicsMagick-${pkgver}
  cp -fHv "${srcdir}"/pathtools.[ch] magick/

  apply_patch_with_msg \
    001-relocate.patch

  # https://sourceforge.net/p/graphicsmagick/bugs/718/
  apply_patch_with_msg \
    002-tiff-transparency.patch

  autoreconf -fiv
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  export lt_cv_deplibs_check_method='pass_all'

  ../GraphicsMagick-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --with-quantum-depth=16 \
    --with-modules \
    --with-wmf \
    --with-xml \
    --with-lcms2 \
    --with-heif \
    --with-jp2 \
    --with-jxl \
    --with-webp \
    --with-jbig \
    --without-fpx \
    --without-dps \
    --without-perl \
    --without-x

  make
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  make -j1 DESTDIR="${pkgdir}" install
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  # fix path references in some files.
  find ${pkgdir}${MINGW_PREFIX}/bin -name "*-config" -exec \
    sed "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
  find ${pkgdir}${MINGW_PREFIX}/ -name "*.la" -exec \
    sed "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
}
